package io.github.topcatv.mall.model.sys.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.topcatv.mall.common.ao.ReturnObject;
import io.github.topcatv.mall.common.ao.RoleAo;
import io.github.topcatv.mall.common.support.JsonArg;
import io.github.topcatv.mall.model.sys.entity.Role;
import io.github.topcatv.mall.model.sys.service.IRoleService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liuyi
 * @since 2019-10-14
 */
@RestController
@RequestMapping("/sys/role")
public class RoleController {
    @Resource
    IRoleService roleService;

    @DeleteMapping("/{id}")
    public ReturnObject remove(@PathVariable("id") Integer id) {
        return roleService.removeById(id) ? ReturnObject.success() : ReturnObject.error("删除失败");
    }

    @PostMapping("")
    public ReturnObject save(@RequestBody RoleAo roleAo) {
        Role entity = roleAo.toEntity();
        return roleService.save(entity) ? ReturnObject.success().put("id", entity.getId()) : ReturnObject.error("新增失败");
    }

    @PostMapping("/addPermission")
    public ReturnObject addPermission(@JsonArg Integer id, @JsonArg Integer[] permissionIds) {
        roleService.addPermissions(id, permissionIds);
        return ReturnObject.success();
    }

    @GetMapping("")
    public ReturnObject query(long pageNo, long pageSize) {
        IPage<Role> page = roleService.page(new Page<>(pageNo, pageSize));
        return ReturnObject.success().put("roles", page);
    }
}
